package ModelLayer;


import java.util.ArrayList;


/**
 * The test class AddressBookTest.
 *
 * @author  Mads Nielsen
 * @version 12-4-2011
 */
public class AddressBookTest extends junit.framework.TestCase
{

    private Person person1;
    private Person person2;
    
    private AddressBook addressBook;
    
    
    /**
     * Default constructor for test class AddressBookTest
     */
    public AddressBookTest()
    {
    }

    /**
     * Sets up the test fixture.
     *
     * Called before every test case method.
     */
    protected void setUp()
    {
        person1 = new Person("John Long", "Addressevej 1", 1234, "Byen", 12345678);
        person2 = new Person("Anders And", "Paradisæblevej 13", 0001, "Andeby", 13131313);
        addressBook = AddressBook.getInstance();
        addressBook.clear();
    }

    /**
     * Tears down the test fixture.
     *
     * Called after every test case method.
     */
    protected void tearDown()
    {
    }
    
    /**
     * Tests that persons added to the addressbook is assigned an id
     */
    public void testAddPersonIdAssignment() {
        
        assertEquals(0, person1.getId());
        
        addressBook.addPerson(person1);
        
        assertEquals(1, person1.getId());
        
    }
    
    /**
     * Test for fetching Person objects from AddressBook by supplying Person id
     */
    public void testGetPerson() {
        
        addressBook.addPerson(person1);
        
        Person get = addressBook.getPerson(person1.getId());
        assertSame(person1, get);
        
    }
    
    /**
     * Test for containsPerson() method
     */
    public void testContainsPerson() {
        
        assertFalse(addressBook.containsPerson(person1));
        
        addressBook.addPerson(person1);
        assertTrue(addressBook.containsPerson(person1));
    }
    
    /**
     * Test for removePerson() method
     */
    public void testRemovePerson() {
        
        addressBook.addPerson(person1);
        
        assertTrue(addressBook.removePerson(person1));
        
        assertFalse(addressBook.containsPerson(person1));
        
    }
    
    /**
     * Test for searching addressbook by name
     */
    public void testSearch() {
        
        addressBook.addPerson(person1);
        addressBook.addPerson(person2);
        
        ArrayList<Person> results = addressBook.search("long");
        
        assertTrue(results.contains(person1));
        assertFalse(results.contains(person2));
        
    }
    
}
